﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ActiveStory.UI
{
    /// <summary>
    /// Interaction logic for LinkDialog.xaml
    /// </summary>
    public partial class LinkDialog : Window
    {
        private int _targetPageNumber;
        public int TargetPageNumber
        {
            get
            {
                return _targetPageNumber;
            }
        }
        public enum LinkDialogModes { CreateLink, RemoveLink, Both };
        public LinkDialog(LinkDialogModes linkDialogMode, int targetPageNumber)
        {
            InitializeComponent();
            if (linkDialogMode == LinkDialogModes.CreateLink || linkDialogMode == LinkDialogModes.Both)
            {
                
                PageListBox.ItemsSource = ActiveStoryApplication.CurrentProject.PageMetadataList;
                if (linkDialogMode == LinkDialogModes.CreateLink)
                {
                    MainTabControl.SelectedIndex = 0;
                    ((TabItem)MainTabControl.Items[1]).Visibility = Visibility.Hidden;
                }
                if (targetPageNumber != -1)
                {
                    PageToLinkQuestionLabel.Content = ((String)PageToLinkQuestionLabel.Content) + " (Currently linked to: " + (targetPageNumber + 1).ToString() + ")";
                    if (ActiveStoryApplication.CurrentProject.PageLookupTable.ContainsKey(targetPageNumber))
                        PageListBox.SelectedItem = ActiveStoryApplication.CurrentProject.PageLookupTable[targetPageNumber];
                }
            }
            if (linkDialogMode == LinkDialogModes.RemoveLink || linkDialogMode == LinkDialogModes.Both)
            {
                
                RemoveLinkImage.Source = new BitmapImage(new Uri(ActiveStoryApplication.CurrentProject.PageLookupTable[targetPageNumber].TempThumbnailPath));
                PageNumberValue.Content = (targetPageNumber + 1).ToString()+"?";
                if (linkDialogMode == LinkDialogModes.RemoveLink)
                {
                    MainTabControl.SelectedIndex = 1;
                    ((TabItem)MainTabControl.Items[1]).Visibility = Visibility.Hidden;
                }
            }
        }

        private void LinkToPageOkButton_Click(object sender, RoutedEventArgs e)
        {
            if (PageListBox.SelectedIndex < 0)
                return;
            _targetPageNumber = ((ActiveStory.DataManager.ActiveStoryPageMetadata)PageListBox.SelectedItem).PageNumber;
            DialogResult = true;
        }

        private void RemoveLinkYesButton_Click(object sender, RoutedEventArgs e)
        {
            _targetPageNumber = -1;
            DialogResult = true;
        }

    }
}
